package cn.wzc.crystal.platform.i18n;

import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

@RequiredArgsConstructor
public class I18nService {

	private final MessageSource messageSource;

	public static Locale getCurrentLocale() {
		return LocaleContextHolder.getLocale();
	}

	public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
		return messageSource.getMessage(resolvable, getCurrentLocale());
	}

	public String getMessage(String code, Object[] args) throws NoSuchMessageException {
		return messageSource.getMessage(code, args, getCurrentLocale());
	}

	public String getMessage(String code) throws NoSuchMessageException {
		return messageSource.getMessage(code, null, getCurrentLocale());
	}

	public String getMessage(String code, Object[] args, String defaultMsg) {
		return messageSource.getMessage(code, args, defaultMsg, getCurrentLocale());
	}

	public String getMessage(String code, String defaultMsg) {
		return getMessage(code, null, defaultMsg);
	}
}